package com.xywy.fwc;

import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.util.MD5Util;
import com.xywy.fwc.bean.FwcHomeData;
import com.xywy.fwc.net.DataCallBack;
import com.xywy.fwc.net.Request;
import com.xywy.fwc.net.StringParser;
import com.xywy.me.bean.UserKeeper;

public class FwcMessageActivity extends BaseActivity implements OnClickListener{
	private RelativeLayout rl_ask_doc;
	private RelativeLayout rl_home_doc;
	private RelativeLayout rl_add;
	private RelativeLayout rl_read;
	private RelativeLayout rl_reduce;
	private RelativeLayout rl_blood;
	
	private ImageView iv_reduce_point;
	private ImageView iv_blood_point;
	private ImageView iv_ask_point;
	private ImageView iv_doc_point;
	private ImageView iv_add_point;
	private SharedPreferences sp;
	private String userid;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fwc_message);
		initView();
		loadData();
	}

	@Override
	public void preInit() {
		
	}

	@Override
	public void loadData() {
	}

	@Override
	public void initView() {
		sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
//		userid = sp.getString("userid", "");
		userid = UserKeeper.getCurrentUser(this).getUserid();
		data = new FwcHomeData();
		findViewById(R.id.tv_title_back).setOnClickListener(this);
		findViewById(R.id.bt_title_back).setOnClickListener(this);
		rl_ask_doc = (RelativeLayout) findViewById(R.id.rl_ask_doc);
		rl_ask_doc.setOnClickListener(this);
		rl_home_doc = (RelativeLayout) findViewById(R.id.rl_home_doc);
		rl_home_doc.setOnClickListener(this);
		rl_add = (RelativeLayout) findViewById(R.id.rl_add);
		rl_add.setOnClickListener(this);
		rl_read = (RelativeLayout) findViewById(R.id.rl_read);
		rl_read.setOnClickListener(this);
		
		rl_reduce = (RelativeLayout) findViewById(R.id.rl_reduce);
		rl_blood = (RelativeLayout) findViewById(R.id.rl_blood);
		rl_reduce.setOnClickListener(this);
		rl_blood.setOnClickListener(this);
		
		iv_reduce_point = (ImageView) findViewById(R.id.iv_reduce_point);
		iv_blood_point = (ImageView) findViewById(R.id.iv_health_point);
		iv_doc_point = (ImageView) findViewById(R.id.iv_doc_point);
		iv_ask_point = (ImageView) findViewById(R.id.iv_ask_point);
		iv_add_point = (ImageView) findViewById(R.id.iv_add_point);
	}
    FwcHomeData data;
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.bt_title_back:
		case R.id.tv_title_back:
			finish();
			break;
		case R.id.rl_add:
			Intent intent = new Intent(this,MyAddActivity.class);
			startActivity(intent);
			FwcConstant.setMessageCount(FwcConstant.STR_MYADD, this, 0);
			clearMessage(FwcConstant.STR_MYADD);
			break;
		case R.id.rl_ask_doc:
			Intent questionIntent = new Intent(this,MyQuestionActivity.class);
			startActivity(questionIntent);
			FwcConstant.setMessageCount(FwcConstant.STR_MYQUESTION, this, 0);
			clearMessage(FwcConstant.STR_MYQUESTION);
			break;
		case R.id.rl_home_doc:
			Intent docIntent = new Intent(this,FwcHomeDoctor.class);
			startActivity(docIntent);
			FwcConstant.setMessageCount(FwcConstant.STR_MYDOCTOR, this, 0);
//			clearMessage(FwcConstant.STR_MYDOCTOR);
			break;
		case R.id.rl_read:
			Intent readIntent = new Intent(this,FwcSubscribeActivity.class);
			startActivity(readIntent);
			break;
		case R.id.rl_reduce:
			Intent fm = new Intent(this, FwcMessageInfo.class);
			data.setName("减肥助手");
			data.setId(6+"");
			fm.putExtra("data", data);
			startActivity(fm);
			FwcConstant.setMessageCount(FwcConstant.STR_MYREDUCE, this, 0);
			clearMessage(FwcConstant.STR_MYREDUCE);
			break;
		case R.id.rl_blood:
//			Intent fm1 = new Intent(this, FwcMessageInfo.class);
//			data.setName("血压管家");
//			data.setId(7+"");
//			fm1.putExtra("data", data);
//			startActivity(fm1);
//			FwcConstant.setMessageCount(FwcConstant.STR_MYHEALTH, this, 0);
//			clearMessage(FwcConstant.STR_MYHEALTH);
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		iv_reduce_point.setVisibility(View.GONE);
		iv_blood_point.setVisibility(View.GONE);
		iv_doc_point.setVisibility(View.GONE);
		iv_ask_point.setVisibility(View.GONE);
		iv_add_point.setVisibility(View.GONE);
		
		int reduce = FwcConstant.getMessageCount(FwcConstant.STR_MYREDUCE, this);
		int blood = FwcConstant.getMessageCount(FwcConstant.STR_MYHEALTH, this);
		int add = FwcConstant.getMessageCount(FwcConstant.STR_MYADD, this);
		int ask = FwcConstant.getMessageCount(FwcConstant.STR_MYQUESTION, this);
		int doc = FwcConstant.getMessageCount(FwcConstant.STR_MYDOCTOR, this);
		
		if(reduce >0){
			iv_reduce_point.setVisibility(View.VISIBLE);
		}else {
			iv_reduce_point.setVisibility(View.GONE);
		}
		if(blood >0){
			iv_blood_point.setVisibility(View.VISIBLE);
		}else {
			iv_blood_point.setVisibility(View.GONE);
		}
		if(add >0){
			iv_add_point.setVisibility(View.VISIBLE);
		}else {
			iv_add_point.setVisibility(View.GONE);
		}
		if(ask >0){
			iv_ask_point.setVisibility(View.VISIBLE);
		}else {
			iv_ask_point.setVisibility(View.GONE);
		}
		if(doc >0){
			iv_doc_point.setVisibility(View.VISIBLE);
		}else {
			iv_doc_point.setVisibility(View.GONE);
		}
	}
	
	
	public void clearMessage(String menu_id){
		try {
			String tag = "tag";
			String sign = MD5Util.md5("KHy69gsk8%#@kl$"+tag);
			HashMap<Object, Object> params = new HashMap<Object, Object>();
			params.put("tag", tag);
			params.put("sign", sign);
			params.put("xywy_userid", userid);
			params.put("menu_id", menu_id);
			Request.postData(new StringParser(), new DataCallBack<String>() {

				@Override
				public void dataCall(String str) {
					try {
						System.out.println(str);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}, FwcConstant.CLEAR_MESSAGE_URL, params);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
